import { View, Text } from "@tarojs/components";
import { getCurrentInstance } from "@tarojs/taro";
import React from "react";
import { AtIconBaseProps } from "taro-ui/types/base";
import { AtList, AtListItem } from "taro-ui";
import { Card } from "../../components/layout";
import { useSetTitle } from "../../hooks";
import { processCity, UserAddress } from "../../util";
import { userAddress } from "../../type";
import Taro from "@tarojs/taro";
const UNKNOW_SENDER = '添加寄件人';
const UNKNOW_RECIPIENT = '添加收件人';
let senderAddressList: userAddress = UserAddress.get('sender')[0] || {
  address: [],
  name: UNKNOW_SENDER,
  phone: '',
  postcode: '',
  detail: ''
};
let recipientAddressList: userAddress = UserAddress.get('recipient')[0] || {
  address: [],
  name: UNKNOW_RECIPIENT,
  phone: '',
  postcode: '',
  detail: ''
};
function init() {
  let param = getCurrentInstance().router.params;
  let data: {
    obj: {
      value: string,
      iconInfo: AtIconBaseProps
    },
    index: number
  } = {
    'obj': JSON.parse(param.obj),
    'index': parseInt(param.index)
  };
  param = null;
  useSetTitle(data.obj.value);
}
function chooseAddress(args: userAddress, type: 'sender' | 'recipient') {
  console.log(args.name);
  if (args.name === UNKNOW_RECIPIENT) {
    return Taro.navigateTo({
      url: '/pages/create-address/index?type=recipient'
    })
  }
  if (args.name === UNKNOW_SENDER) {
    return Taro.navigateTo({
      url: '/pages/create-address/index?type=sender'
    })
  }
}

export default function () {
  init();
  return (
    <View>
      <Card>
        <View>
          <Text style="padding-left:5px;margin-left:5px" className='fsbase'>收件人</Text>
        </View>
        <AtList>
          <AtListItem title={recipientAddressList.name} note={processCity(recipientAddressList.address.concat(recipientAddressList.detail))} extraText={recipientAddressList.phone}
            onClick={chooseAddress.bind(this, recipientAddressList, 'recipient')}>
          </AtListItem>
        </AtList>
      </Card>
      <Card>
        <View>
          <Text style="padding-left:5px;margin-left:5px" className='fsbase'>寄件人</Text>
        </View>
        <AtList>
          <AtListItem title={senderAddressList.name} note={processCity(senderAddressList.address.concat(senderAddressList.detail))} extraText={senderAddressList.phone} onClick={chooseAddress.bind(this, senderAddressList, 'sender')}>
          </AtListItem>
        </AtList>
      </Card>
    </View>
  )
}